package com.heitian.ssm.controller;

import com.alibaba.fastjson.JSON;
import com.heitian.ssm.Constants.WebConstants;
import com.heitian.ssm.model.User;
import com.heitian.ssm.model.UserEntity;
import com.heitian.ssm.service.UserLoginService;
import com.heitian.ssm.service.UserService;
import com.heitian.ssm.utils.*;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: mishuai
 * Date: 2018-04-22
 * Time: 01:00
 * Des:
 */
@Controller
@RequestMapping("/login")
public class LoginController {
    private Logger log = Logger.getLogger(UserController.class);
    @Resource
    private UserLoginService userLoginService;

    @RequestMapping("/showUser")
    @ResponseBody
    public void showUser(HttpServletRequest request, HttpServletResponse response, @RequestParam String userName,@RequestParam String passwd)  {
        log.info("查询所有用户信息入参"+userName);

        UserEntity userEntity=userLoginService.login(userName,passwd);
        if(userEntity !=null){
            JsonResultUtils.ajaxReturn(response,FastJsonUtils.toJSONString(Result.successReturn(userEntity)));
        }else {
            JsonResultUtils.ajaxReturn(response, FastJsonUtils.toJSONString(Result.falseReturnCodeMsg("F_99999","登录失败")));
        }


    }
    /*, @RequestParam String username1,@RequestParam String card,@RequestParam String className,@RequestParam String email1,@RequestParam String pass*/
    @RequestMapping("/registe")
    @ResponseBody
    public void registe(HttpServletRequest request, HttpServletResponse response){

        String username=request.getParameter("username1");
        String card=request.getParameter("card");
        String className=request.getParameter("calssName");
        String email=request.getParameter("email1");
        String passwd=request.getParameter("pass");
       log.info("查询所有用户信息入参"+username+card+className+email+passwd);
        UserEntity userEntity=new UserEntity();
        userEntity.setUserName(username);
        userEntity.setUserCardNo(card);
        userEntity.setPwdSalt(WebConstants.SALT);
        userEntity.setUserEmail(email);
        userEntity.setClassName(className);
        byte by=(byte)1;
        userEntity.setIsStatus(by);
        userEntity.setCreateTime(new Date());
        userEntity.setModifyTime(new Date());
        String md5Passwd=null;
        try {
            md5Passwd= MD5.md5(passwd,WebConstants.SALT);
        } catch (Exception e) {
            e.printStackTrace();
        }

        log.info("入参"+FastJsonUtils.toJSONString(userEntity));
        userEntity.setUserPwd(md5Passwd);
        int i=0;
        try {
            i=userLoginService.register(userEntity);
        }catch (Exception e){
            log.error(e);
        }

        if(i<=0){
            JsonResultUtils.ajaxReturn(response, FastJsonUtils.toJSONString(Result.falseReturnCodeMsg("F_99999","插入数据失败")));
        }else {
            try {
                MailUtil.send_mail(email,"尊敬的"+username+"用户：感谢您注册我们的网站");
            } catch (MessagingException e) {
                e.printStackTrace();
            }
            JsonResultUtils.ajaxReturn(response,FastJsonUtils.toJSONString(Result.successReturn(userEntity)));
        }



    }
}
